package dev.langchain4j.example.agent;

import dev.langchain4j.example.tools.TASKTYPE;
import dev.langchain4j.service.TokenStream;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    private final EmbeddingService embeddingService;

    private final CustomerSupportAgent customerSupportAgent;

    public TestController(EmbeddingService embeddingService, CustomerSupportAgent customerSupportAgent) {
        this.embeddingService = embeddingService;
        this.customerSupportAgent = customerSupportAgent;
    }


    @Resource
    private Agents.GreetingExpert greetingExpert;

    @Resource
    private Agents.ChatBot chatBot;

    public String handle(String userMessage) {

        TASKTYPE task = greetingExpert.isTask(userMessage);
        switch (task) {
            case FUNCTIONCALL:
                log.info("FUNCTIONCALL");

            case WEBSEARCH:
                log.info("WEBSEARCH");
                return chatBot.reply(userMessage);
        }
        return null;
    }

    @RequestMapping(value = "/dddddchat", produces = "text/stream;charset=UTF-8")
    public Flux<String> stream(@RequestParam String message) {

        String sessionId = String.valueOf((Math.random() * 100));

        TokenStream stream = customerSupportAgent.stream(sessionId, message, LocalDate.now().toString());
        return Flux.create(sink -> {
            stream.onPartialResponse(s -> {
                        sink.next(s);
                    })
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });


    }


} 